package com.example.cny.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.cny.R;
import com.example.cny.adapter.FragmentTabAdapter;
import com.example.cny.fragment.CompetitionFragment;
import com.example.cny.fragment.DataFragment;
import com.example.cny.fragment.FightingFragment;
import com.example.cny.fragment.GoFragment;
import com.example.cny.fragment.HomeFragment;
import com.example.cny.presenter.MainPresenter;
import com.example.mylibrary.base.BaseActivity;
import com.example.mylibrary.base.BaseNoDaggerActivity;
import com.example.mylibrary.di.component.BaseComponent;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseNoDaggerActivity {
    @BindView(R.id.tv_tab_one)
    TextView tvTabOne;
    @BindView(R.id.tv_tab_two)
    TextView tvTabTwo;
    @BindView(R.id.tv_tab_four)
    TextView tvTabFour;
    @BindView(R.id.tv_tab_five)
    TextView tvTabFive;

    @BindView(R.id.iv_tab_one)
    ImageView ivTabOne;
    @BindView(R.id.iv_tab_two)
    ImageView ivTabTwo;
    @BindView(R.id.iv_tab_three)
    ImageView ivTabThree;
    @BindView(R.id.iv_tab_four)
    ImageView ivTabFour;
    @BindView(R.id.iv_tab_five)
    ImageView ivTabFive;

    private ArrayList<Fragment> fragments;
    private FragmentTabAdapter tabAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        initFragment();
        initListener();
    }

    private void initFragment(){
        fragments = new ArrayList<>();
        fragments.add(HomeFragment.newInstance());
        fragments.add(DataFragment.newInstance());
        fragments.add(GoFragment.newInstance());
        fragments.add(FightingFragment.newInstance());
        fragments.add(CompetitionFragment.newInstance());
        tabAdapter = new FragmentTabAdapter(this, fragments, R.id.fl_layout) ;
    }

    /**
     *  底部导航栏的点击事件
     * @param view
     */
    @OnClick({R.id.ll_tab_one,R.id.ll_tab_two,R.id.ll_tab_three,R.id.ll_tab_four,R.id.ll_tab_five})
    public void onClicked(View view) {
        switch (view.getId()){
            case R.id.ll_tab_one:
                tabAdapter.setCurrentFragment(0);
                break;
            case R.id.ll_tab_two:
                tabAdapter.setCurrentFragment(1);
                break;
            case R.id.ll_tab_three:
                tabAdapter.setCurrentFragment(2);
                break;
            case R.id.ll_tab_four:
                tabAdapter.setCurrentFragment(3);
                break;
            case R.id.ll_tab_five:
                tabAdapter.setCurrentFragment(4);
                break;
        }
    }

    protected void initListener() {
        tabAdapter.setOnTabChangeListener(new FragmentTabAdapter.OnTabChangeListener() {
            @Override
            public void OnTabChanged(int index) {
                tvTabOne.setTextColor(getResources().getColor(R.color.tab_normal));
                tvTabTwo.setTextColor(getResources().getColor(R.color.tab_normal));
                tvTabFour.setTextColor(getResources().getColor(R.color.tab_normal));
                tvTabFive.setTextColor(getResources().getColor(R.color.tab_normal));

                ivTabOne.setImageResource(R.mipmap.home_normal);
                ivTabTwo.setImageResource(R.mipmap.data_normal);
                ivTabThree.setImageResource(R.mipmap.go_normal);
                ivTabFour.setImageResource(R.mipmap.fighting_normal);
                ivTabFive.setImageResource(R.mipmap.competition_normal);

                switch (index){
                    case 0:
                        tvTabOne.setTextColor(getResources().getColor(R.color.tab_pressed));
                        ivTabOne.setImageResource(R.mipmap.home_pressed);
                        break;
                    case 1:
                        tvTabTwo.setTextColor(getResources().getColor(R.color.tab_pressed));
                        ivTabTwo.setImageResource(R.mipmap.data_pressed);
                        break;
                    case 2:
//                        tvTabThree.setTextColor(getResources().getColor(R.color.tab_pressed));
                        ivTabThree.setImageResource(R.mipmap.go_pressed);
                        break;
                    case 3:
                        tvTabFour.setTextColor(getResources().getColor(R.color.tab_pressed));
                        ivTabFour.setImageResource(R.mipmap.fighting_pressed);
                        break;
                    case 4:
                        tvTabFive.setTextColor(getResources().getColor(R.color.tab_pressed));
                        ivTabFive.setImageResource(R.mipmap.competition_pressed);
                        break;
                }

            }
        });
    }

}
